<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
require_once(dirname(__FILE__) . "/../dbcom/WorkerCom.php");

$workercom = new WorkerCom();

$cont = $_REQUEST['cont'];
switch ($cont) {
    case 1://增
        $account = $_REQUEST['username'];
        $password = $_REQUEST['password'];
        $community = $_REQUEST['community'];
        $oldaccount = $workercom->getPassword($account);
        if (!empty($oldaccount)) {
            echo json_encode(array("code" => 100, "msg" => "account has been used"));
            break;
        } else {
            $res = $workercom->addWorker($account, md5($password), $community);
            if ($res) {
                echo json_encode(array("code" => 0, "msg" => "success"));
            } else {
                echo json_encode(array("code" => 400, "msg" => "add error"));
            }
        }
        break;
    case 2://查
        $workerid = $_REQUEST['workerid'];
        if(empty($workerid)){
            echo json_encode(array("code" => 100, "msg" => "empty workerid"));
            break;
        }
        $worker = $workercom->getUserById($workerid);
        $result = array("code"=>0,"msg"=>"success","data"=>array());
        if(!$worker){
            echo json_encode(array("code" => 400, "msg" => "search worker failed"));
            break;
        }
        $result['data']['community'] = $worker['community_id'];
        $result['data']['username'] = $worker['account'];
        echo json_encode($result);
        break;
    case 3://改
        $workerid = $_REQUEST['workerid'];
        $community = $_REQUEST['community'];
        if(empty($workerid)||empty($community)){
            echo json_encode(array("code" => 100, "msg" => "empty workerid or community"));
            break;
        }
        if(!isset($_REQUEST['password'])||empty($_REQUEST['password'])){
            $res = $workercom->updateWorker($workerid, $community);
        }else{
            $res = $workercom->updateWorker($workerid, $community,md5($_REQUEST['password']));
        }
        if($res){
            echo json_encode(array("code" => 0, "msg" => "success"));
        }else{
            echo json_encode(array("code" => 400, "msg" => "update error"));
        }
        break;
    case 4://删
        $workerid = $_REQUEST['worker'];
        if(empty($workerid)){
            echo json_encode(array("code" => 100, "msg" => "empty workerid"));
            break;
        }
        $res = $workercom->deleteWorker($workerid);
        if($res){
            echo json_encode(array("code" => 0, "msg" => "success","data"=>$workerid));
        }else{
            echo json_encode(array("code" => 400, "msg" => "delete error"));
        }
        break;
    default :
        echo json_encode(array("code" => 500, "msg" => "undefined error"));
        break;
}
exit();
